package com.example.imagediscerndemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;



public class LoginActivity extends AppCompatActivity {

    private Button button;
    private EditText editText1;
    private EditText editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        editText1 = (EditText)findViewById(R.id.et_user_name);
        editText2 = (EditText)findViewById(R.id.et_psw);
        button = (Button)findViewById(R.id.btn_login);
        /**初始化EditText,默认都为未选中状态**/
        editText1.setBackgroundResource(R.drawable.et_underline_unselected);
        editText2.setBackgroundResource(R.drawable.et_underline_unselected);
       /**第一个EditText的焦点监听事件**/
        editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Log.e((String)v.getTag(), "EditText1获得焦点");
                    editText1.setBackgroundResource(R.drawable.et_underline_selected);

                } else {
                    Log.e((String)v.getTag(), "EditText1失去焦点");
                    editText1.setBackgroundResource(R.drawable.et_underline_unselected);
                }
            }
        });
        /**第二个EditText的焦点监听事件**/
        editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Log.e((String)v.getTag(), "EditText2获得焦点");
                    editText2.setBackgroundResource(R.drawable.et_underline_selected);

                } else {
                    Log.e((String)v.getTag(), "EditText2失去焦点");
                    editText2.setBackgroundResource(R.drawable.et_underline_unselected);
                }
            }
        });
        /**button点击事件**/
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editText1.getText().toString();
                String password = editText2.getText().toString();
                if(username.equals("jim")&&password.equals("12345")) {
                    Intent it = new Intent();
                    it.setClass(LoginActivity.this, MainActivity.class);
                    LoginActivity.this.startActivity(it);
                }
                else {
                    showMsg("用户名或密码错误！");
                }
            }
        });
    }

    private void showMsg(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
